package com.grocery.controller;

import ch.qos.logback.core.Layout;
import com.grocery.bean.Product;
import com.grocery.dao.ProductDao;
import com.grocery.service.impl.ProductService;
import com.grocery.util.LayData;
import com.grocery.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text) {
        List<Product> list = productService.getList(page,text);
        int count = productService.getCount(text);
        return new LayData(list, count);
    }

    @RequestMapping("getlist")
    @ResponseBody
    public List<Product> getAllList() {
        return productService.getAllList();
    }

    @RequestMapping("getlistbypid")
    @ResponseBody
    public List<Product> getAllListbyPid(Integer pid) {
        return productService.getAllListbypid(pid);
    }


    @RequestMapping("add")
    @ResponseBody
    public int add(Product product) {
        return productService.add(product);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int id) {
        return productService.delete(id);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int edit(Product product) {
        return productService.edit(product);
    }

    @RequestMapping("select")
    @ResponseBody
    public Product getByID(int id) {
        return productService.getByID(id);
    }

    @RequestMapping("state")
    @ResponseBody
    public List<Product> getByState(int state) {
        return productService.getByState(state);
    }



}
